<template>

  <transition name="slide">
    <music-list :title="title" :bgImage="bgImage" :songs="songs"></music-list>

  </transition>

</template>

<script type="text/ecmascript-6">
  import MusicList from 'components/music-list/music-list'
  import {mapGetters} from 'vuex'
  import {getSongList} from 'api/recommend'
  import {ERR_OK} from 'api/config'
  import {createSong,getSongVkey} from 'common/js/song'



  export default {
    name: "disc",
    created(){
      this._getSongList()
    },
    data() {
      return {
        songs: []
      }
    },
    computed: {
      title() {
        return this.disc.dissname
      },
      bgImage() {
        return this.disc.imgurl
      },
      ...mapGetters([
        'disc'
      ])
    },
    methods:{
      _getSongList(){
        if (!this.disc.dissid){
          this.$router.push('/recommend')
          return
        }

        getSongList(this.disc.dissid).then((res)=>{
          if (res.code ===ERR_OK ){

            this.songs = this._formatSongs(res.cdlist[0].songlist)

          }
        })
      },
      //重组 res.data.list 数据,只拿需要的
      _formatSongs(list){
        let result = []
        list.forEach((item)=>{
          getSongVkey(item.songmid).then((res) => {
            const vkey = res.data.items[0].vkey;
            if (item.songid && item.albummid) {
              result.push(createSong(item, vkey))
            }
          })

        })
        return result
      },
      _normalizeSongs(list){
        let ret =[]
        list.forEach((musicData)=>{
          if (musicData.songid&&musicData.albummid){
            ret.push(createSong(musicData))
          }
        })
        return ret
      }
    },
    components: {
      MusicList
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
